﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookStore
{
    internal class sqlHelper
    {
        public int ExecuteNonQuery(string command, SqlConnection sqlConnection, params SqlParameter[] parameters)
        {
            using (SqlCommand cmd = new SqlCommand(command, sqlConnection))
            {
                cmd.Parameters.AddRange(parameters);
                sqlConnection.Open();
                int result = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                sqlConnection.Close();
                return result;
            }
        }

        public object ExecuteScalar(string command, SqlConnection sqlConnection, params SqlParameter[] parameters)
        {
            using (SqlCommand cmd = new SqlCommand(command, sqlConnection))
            {

                cmd.Parameters.AddRange(parameters);
                sqlConnection.Open();
                object result = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                sqlConnection.Close();
                return result;
            }
        }

        public object CheckSameUsernameOrID(SqlConnection sqlConnection, params SqlParameter[] parameters)
        {
            string sqlSelectCommand = "Select username,id from BookStore.dbo.userinfo where username = @username or id =  @id";
            return ExecuteScalar(sqlSelectCommand, sqlConnection, parameters);
        }
        public int Register(SqlConnection sqlConnection, params SqlParameter[] parameters)
        {
            string sqlInserCommand = "insert into BookStore.dbo.userinfo values(@username,@password,@id,@date)";
            return ExecuteNonQuery(sqlInserCommand, sqlConnection, parameters);
        }
    }
        /*
        public SqlDataReader ExecuteReader(string command, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(_connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(command, conn))
                {
                    cmd.Parameters.AddRange(parameters);
                    conn.Open();
                    SqlDataReader result = cmd.ExecuteReader();
                    cmd.Parameters.Clear();
                    return result;
                }
            }
        }
        */

 }

